<template>
	<view class="page index">
		<view class="header">
			<common-search v-model="keyWord" @input="input" @filter="openPopup"></common-search>
		</view>
		<view class="main">
			<empty v-if="empty" :desc="desc"></empty>
			<scroll-view v-else class="scroll" scroll-y="true" :show-scrollbar="false" :enhanced="true" @scrolltolower="scrollLower">
				<view class="project-list">
					<view class="project-item" v-for="item in projectList" :key="item.id" @click="toDetails(item.id)">
						<view class="header">
							<view class="left">
								{{item.name || ''}}
							</view>
							<view class="right" :style="{color: `${item.color}`, borderColor: `${item.color}`}">
								{{item.statusText}}
							</view>
						</view>
						<view class="pro-code">
							项目编号：{{item.projectNo}}
						</view>
						<view class="details">
							<view class="det-item">
								<view class="tag">
									项目类型
								</view>
								<view class="value">
									{{item.typeText}}
								</view>
							</view>
							<view class="det-item">
								<view class="tag">
									项目等级
								</view>
								<view class="value">
									{{item.levelText}}
								</view>
							</view>
							<!-- <view class="det-item">
								<view class="tag">
									项目联系人
								</view>
								<view class="value">
									xxxx
								</view>
							</view> -->
							<view class="det-item">
								<view class="tag">
									项目负责人
								</view>
								<view class="value">
									{{item.ownerUserName || ''}}
								</view>
							</view>
							<view class="det-item">
								<view class="tag">
									当前阶段
								</view>
								<view class="value">
									{{item.stageText || ''}}
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="create" @click="createProject">
				新建
			</view>
			<uni-popup ref="projectPopup" type="top" background-color="#fff">
				<view class="filter">
					<view class="filter-container status">
						<view class="title">
							项目状态
						</view>
						<view class="filter-list">
							<view :class="['item', statusIndex == index && 'select']" v-for="(item, index) in statusList" :key="index" @click="statusIndex = index">
								{{item}}
							</view>
						</view>
					</view>
					<!-- <view class="filter-container team">
						<view class="title">
							项目团队
						</view>
						<view class="filter-list">
							<view :class="['item', teamIndex == index && 'select']" v-for="(item, index) in teamList" :key="index" @click="teamIndex = index">
								{{item}}
							</view>
						</view>
					</view> -->
					<view class="filter-container time">
						<view class="title">
							任务下发时间
						</view>
						<view class="select-time">
							<uni-datetime-picker v-model="timeRange" type="daterange" :end="Date.now()" rangeSeparator="至" @change="changeTime" />
						</view>
					</view>
					<view class="footer">
						<button class="cencle" @click="cencle">清空</button>
						<button class="confire" @click="confireFilter">确定</button>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import commonSearch from "@/components/commonSearch.vue";
	import empty from "@/components/empty.vue";
	import {getProjectListApi} from "@/api/project_api.js";
	export default {
		components: {
			commonSearch,
			empty
		},
		data() {
			return {
				empty: false,
				desc: '',
				keyWord: '',
				statusIndex: -1,
				statusList: ['进行', '暂停', '作废', '正常完结', '异常完结'],
				// teamIndex: -1,
				// teamList: ['我负责的', '下属负责的', '我参与的', '下属参与的'],
				timeRange: [],
				projectList: [],
				pageNum: 1,
				pageSize: 10,
				totalPage: 1
			}
		},
		onShow() {
			uni.showLoading({
				mask: true,
				title: '加载中...'
			})
			this.projectList = [];
			this.pageNum = 1;
			this.getProjectList();
		},
		methods: {
			scrollLower(){
				if(this.pageNum >= this.totalPage){
					return;
				}
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				this.pageNum++;
				this.getProjectList();
			},
			// 获取项目列表
			async getProjectList(){
				this.empty = false;
				if(this.keyWord == ''){
					this.desc = '暂无项目'
				}else{
					this.desc = '未搜索到项目'
				}
				let res = await getProjectListApi({
					name: this.keyWord,
					pageNo: this.pageNum,
					pageSize: this.pageSize,
					status: this.statusIndex < 0 ? '' : this.statusIndex,
					createTime: this.timeRange.length == 0 ? '' : `${this.timeRange[0]} 00:00:00, ${this.timeRange[1]} 00:00:00`
				}).catch(() => {
					uni.hideLoading();
					this.empty = true;
					return Promise.reject();
				})
				res.data.list = res.data.list.map((item) => {
					let typeText = '';
					let levelText = '';
					let statusText = '';
					let color = '';
					let stageText = ''
					switch(item.type){
						case 0: {
							typeText = '产品开发类';
							break;
						}
						case 1: {
							typeText = '运维服务类';
							break;
						}
						case 2: {
							typeText = '设备销售类';
							break;
						}
					}
					switch(item.level){
						case 0: {
							levelText = 'A（重要）';
							break;
						}
						case 1: {
							levelText = 'B（关注）';
							break;
						}
						case 2: {
							levelText = 'C（一般）';
							break;
						}
					}
					switch(item.status){
						case 0: {
							statusText = '进行';
							color = '#3388FF';
							break;
						}
						case 1: {
							statusText = '暂停';
							color = '#DE868F';
							break;
						}
						case 2: {
							statusText = '作废';
							color = '#C9C9C9';
							break;
						}
						case 3: {
							statusText = '正常完结';
							color = '#3BBF03';
							break;
						}
						case 4: {
							statusText = '异常完结';
							color = '#951D1D';
							break;
						}
					}
					switch(item.stage){
						case 0: {
							stageText = '意向';
							break;
						}
						case 1: {
							stageText = '签约';
							break;
						}
						case 2: {
							stageText = '实施';
							break;
						}
						case 3: {
							stageText = '交付';
							break;
						}
						case 4: {
							stageText = '回款';
							break;
						}
						case 5: {
							stageText = '归档';
							break;
						}
					}
					return{
						...item,
						typeText,
						levelText,
						statusText,
						color,
						stageText
					}
				})
				this.projectList = [...this.projectList, ...res.data.list];
				this.totalPage = Math.ceil(res.data.total / this.pageSize);
				if(this.projectList.length == 0){
					this.empty = true;
				}
				await uni.hideLoading();
			},
			//关键字
			input(e) {
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				this.projectList = [];
				this.pageNum = 1;
				this.getProjectList();
			},
			// 清空筛选
			cencle(){
				this.statusIndex = -1;
				this.timeRange = [];
			},
			//确定筛选
			confireFilter(){
				this.$refs.projectPopup.close();
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				this.projectList = [];
				this.pageNum = 1;
				this.getProjectList();
			},
			changeTime(e){
				console.log(this.timeRange,e, '==============')
			},
			openPopup(){
				this.$refs.projectPopup.open();
			},
			toDetails(id) {
				uni.navigateTo({
					url: `/pages/index/project/details?id=${id}`
				})
			},
			createProject() {
				uni.navigateTo({
					url: '/pages/index/project/createProject'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/style/customicons.scss";

	.index {
		@include flex-c;

		>.header {
			padding: 24rpx 30rpx 0;
			flex: 0 0 auto;
		}

		.main {
			flex: 1 0 0;
			overflow: hidden;

			.scroll{
				height: 100%;
				.project-list {
					padding: 24rpx 30rpx;
				
					.project-item {
						@include box;
						@include list;
						margin: 0 0 18rpx;
						.pro-code{
							color: #9A9A9A;
							font-size: 24rpx;
						}
					}
				}
			}

			.create {
				position: fixed;
				bottom: 106rpx;
				right: 30rpx;
				width: 80rpx;
				height: 80rpx;
				background-color: #4095E5;
				@include flex-r-center-center;
				font-size: 24rpx;
				color: #fff;
				border-radius: 50%;
				box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
			}
			.filter {
				@include popup;
				.time{
					.select-time{
						padding: 20rpx 0 0;
					}
				}
			}

			&::-webkit-scrollbar {
				display: none;
			}
		}
	}
	::v-deep .uni-calendar--fixed {
		top: 0;
		bottom: unset;
	}
</style>